package com.demo2do.boyle.client.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.service.AdvertisementTagService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.advertisement.Status;
import com.demo2do.boyle.common.entity.customer.CustomerTag;
import com.demo2do.boyle.common.entity.customer.social.SocialType;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * @author mohanlan
 */
@Service("advertisementTagService")
public class AdvertisementTagServiceImpl extends GenericServiceImpl<Advertisement> implements AdvertisementTagService{
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.AdvertisementTagService#listByCutomerTag(com.demo2do.boyle.common.entity.customer.CustomerTag, com.demo2do.core.web.resolver.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<Advertisement> listByCutomerTag(CustomerTag customerTag,
			Page page, SocialType socialType) {
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("status", Status.HANDLING);
		parameters.put("nowDate", new Date());
		
		String sentence = "SELECT advertisementTag.advertisement FROM AdvertisementTag advertisementTag WHERE "
						  + "advertisementTag.advertisement.status = :status and advertisementTag.advertisement.endDate > :nowDate";
		
		if(customerTag.getAgeTag() != 0)  {
			parameters.put("ageTag", customerTag.getAgeTag());
			sentence = sentence + " AND bitand(advertisementTag.ageTag, :ageTag) <> 0";
		}
		
		if(customerTag.getSexTag() != 0) {
			parameters.put("sexTag", customerTag.getSexTag());
			sentence = sentence + " AND bitand(advertisementTag.sexTag, :sexTag) <> 0"; 
		}
		
		if(customerTag.getLifeTag() !=0) {
			parameters.put("lifeTag", customerTag.getLifeTag());
			sentence = sentence + " AND ( ( bitand(advertisementTag.lifeTag, :lifeTag) <> 0) OR (advertisementTag.lifeTag = 0 ))";
		}
		
		if(customerTag.getInterestTag() != 0) {
			parameters.put("interestTag", customerTag.getInterestTag());
			sentence = sentence + " AND ( ( bitand(advertisementTag.interestTag, :interestTag) <> 0 ) OR (advertisementTag.interestTag = 0 ))"; 
		}
		
		if(customerTag.getAreaTag() != 0) {
			parameters.put("areaTag", customerTag.getAreaTag());
			sentence = sentence + " AND ( ( bitand(advertisementTag.areaTag, :areaTag) <> 0) OR ( advertisementTag.areaTag = 0 ))"; 
		}

		if(socialType != null) {
			parameters.put("socialType", socialType);
			sentence = sentence + " AND advertisementTag.advertisement.socialType = :socialType";
		}
		
		List<Advertisement> result = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getMaxResultRecords());
		
		return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
	}

}
